from selenium.webdriver.common.by import By
import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from web import DButils


class Mcard:
    def addcard(self, driver, username, password, bank, type, cardNum):
        driver.find_element(By.ID, "username").send_keys(username)
        driver.find_element(By.ID, "password").send_keys(password)
        driver.find_element(By.ID, "login_btn").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/a").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        driver.find_element(By.ID, "bankCard_add_modal_btn").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div.modal-content"))
        )
        driver.find_element(By.ID, "cardBank_add_input").send_keys(bank)
        time.sleep(1)
        btn = driver.find_element(By.XPATH, "//label[.//input[@value='{}']]".format(type))
        btn.click()
        time.sleep(1)
        driver.find_element(By.ID, "cardNum_add_input").send_keys(cardNum)
        time.sleep(1)
        driver.find_element(By.ID, "bankCard_save_btn").click()

    def add_result(self, cardNum, username):
        sql = "select * from bankcard where cardNum=%s and userId=(select id from user where username=%s)"
        param = [cardNum, username]
        result = len(DButils.select(sql, param))
        return result

    def deletecard(self, driver, username, password, cardNum, state):
        driver.find_element(By.ID, "username").send_keys(username)
        driver.find_element(By.ID, "password").send_keys(password)
        driver.find_element(By.ID, "login_btn").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/a").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        driver.find_element(By.XPATH,
                            "//button[@delete-name='{}']".format(cardNum)).click()
        time.sleep(1)
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable(
                (By.XPATH,
                 "//button[normalize-space()='{}']".format(state))
            )
        )
        time.sleep(1)
        driver.find_element(By.XPATH,
                            "//button[normalize-space()='{}']"
                            .format(state)).click()

    def updatecard(self, driver, username, password, bank, type, cardNum1, cardNum):
        driver.find_element(By.ID, "username").send_keys(username)
        driver.find_element(By.ID, "password").send_keys(password)
        driver.find_element(By.ID, "login_btn").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/a").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        sibling_btn = driver.find_element(
            By.XPATH,
            "//button[@delete-name='{}']/preceding-sibling::button[@class='btn btn-primary update_btn']".format(cardNum)
        )
        sibling_btn.click()
        time.sleep(2)
        if bank != '无':
            driver.find_element(By.ID, "cardBank_update_input").clear()
            driver.find_element(By.ID, "cardBank_update_input").send_keys(bank)
        if cardNum1 != '无':
            driver.find_element(By.ID, "cardNum_update_input").clear()
            driver.find_element(By.ID, "cardNum_update_input").send_keys(cardNum1)
        if type != '无':
            driver.find_element(By.XPATH,
                                "//*[@id='bankCardUpdateModal']/div/div/div[2]/form/div[2]/div[1]/div/label[{}]"
                                .format(type)).click()
        driver.find_element(By.ID, "bankCard_update_btn").click()
        time.sleep(1)

    def update_result(self, id, bank, type, cardNum):
        if bank != '无':
            sql = "select cardBank from bankcard where id=%s"
        elif type != '无':
            sql = "select type from bankcard where id=%s"
        else:
            sql = "select cardNum from bankcard where id=%s"
        param = [id]
        result = DButils.select(sql, param)[0][0]
        return result
